package com.ruoyi.caipiao.utils;

import cn.hutool.core.date.DateUtil;
import org.junit.Test;

import java.io.File;
import java.util.*;

/**
 * @author wangcen
 * @date 2025/1/23 17:24
 */

public class QxcTest {

    @Test
    public void testNum() {
        Set<String> pc = new TreeSet<>();
        String zj = "69664210";
        String yyyyMMdd = "20250219"; // DateUtil.format(new Date(), "yyyyMMdd");
        String dir = "D:\\qxc\\" + yyyyMMdd + "\\";

        File[] files = new File(dir).listFiles();


        for(int i = 125; i < files.length ; i++) {
//            int ctt = new Random().nextInt(files.length);

            File file = files[i];
            if (file.isDirectory()) {
                continue;
            }
            List<String> s1 = MyFileUtil.readFileAsList(file);
            Set<String> st = new TreeSet<>();
            for (String s : s1) {
                int ct = Integer.parseInt(s.substring(s.indexOf(">") + 1));
                if (ct >= 2) {
                    st.add(s.substring(0, 8));
                }
            }


            pc.addAll(st);
            System.out.println(i+"--->"+file.getName() + "当前排除个数：" + st.size() + ", zj:" + st.contains(zj) +", 合集大小:"+ pc.size());

            if(pc.size() >10000000) {
                MyFileUtil.writeContentToFile(dir + "中_all_nums_"+i+".txt", pc.toString() + "\n", true);
                pc.clear();
            }
        }
        System.out.println("排除个数：" + pc.size() + ", zj:" + pc.contains(zj));
        MyFileUtil.writeContentToFile(dir + "排出_all_nums.txt", pc.toString() + "\n", true);

//        int j = 0;
//        for(int i = 0; i < 100; i++) {
//            String num = QxcNumUtil.getNum();
//            if(! pc.contains(num)) {
//                System.out.println("i:"+i);
//                System.out.println(num);
//                j++;
//            }
//        }
    }

    @Test
    public void testGene() {
        for (int i = 0; i < 1000; i++) {
            cal(i);
        }
    }

    public static void cal(int index) {

        String yyyyMMdd = DateUtil.format(new Date(), "yyyyMMdd");
        String dir = "D:\\qxc\\" + yyyyMMdd + "\\";
        File file = new File(dir);
        if (!file.exists()) {
            file.mkdirs();
        }

        Map<String, Integer> numsMap = new HashMap<>();
        for (int i = 0; i < 5000000; i++) {
            String num = QxcNumUtil.getNum();
            numsMap.merge(num, 1, Integer::sum);
        }
        StringBuilder ss = new StringBuilder();
        int k = 1;
        for (Map.Entry<String, Integer> entry : numsMap.entrySet()) {
            String num = entry.getKey();
            Integer ct = entry.getValue();
            ss.append(num).append("->").append(ct).append("\n");
            if (k % 20000 == 0) {
                MyFileUtil.writeContentToFile(dir + index + "_all_nums.txt", ss.toString(), true);
                ss = new StringBuilder();
            }
            k++;
        }
        MyFileUtil.writeContentToFile(dir + index + "_all_nums.txt", ss.toString(), true);
    }

    @Test
    public void cal2() {
        String f1 = "D:\\qxc\\20250219\\中_all_nums_24.txt";
        String f2 = "D:\\qxc\\20250219\\中_all_nums_49.txt";
        String f3 = "D:\\qxc\\20250219\\中_all_nums_74.txt";
        String f4 = "D:\\qxc\\20250219\\中_all_nums_99.txt";
        String f5 = "D:\\qxc\\20250219\\中_all_nums_124.txt";

        Set<String> s1 = MyFileUtil.readFileToSet(new File(f1));
        Set<String> s2 = MyFileUtil.readFileToSet(new File(f2));
        Set<String> s3 = MyFileUtil.readFileToSet(new File(f3));
        Set<String> s4 = MyFileUtil.readFileToSet(new File(f4));
        Set<String> s5 = MyFileUtil.readFileToSet(new File(f5));

        s1.retainAll(s2);
        s1.retainAll(s3);
        s1.retainAll(s4);
        s1.retainAll(s5);
        System.out.println(s1.size());

        String yyyyMMdd = "20250219"; // DateUtil.format(new Date(), "yyyyMMdd");
        String dir = "D:\\qxc\\" + yyyyMMdd + "\\";
        MyFileUtil.writeContentToFile(dir + "排除——"+s1.size()+".txt", s1.toString() + "\n", true);

//        int i = 0;
//        for(;;) {
//            String num = QxcNumUtil.getNum();
//            if(!s1.contains(num)) { {
//                System.out.println(num);
//                i++;
//                if(i >=5) {
//                    break;
//                }
//            }}
//        }
    }
}
